const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
  data: {
    /**红包 */
    redShow:false,
    redtext:'開',
    dis:true,
    nexttrue:false,
    phoneShow:true,
    sendShow:true,
    sendTxt:'60s',
    phone:'',
    truephone:'',
    time:60,
    setInter:'',
    wxCode:'',//服务器返回的code
    phoneCode:'',//用户输入的code
  },
  onShow: function (){
    if(wx.getStorageSync('uid')){
      this.getPhone()
    }else{
      wx.switchTab({
        url: '../index/index'
      })
    }
    //缓存中是否 手机号  验证码
    if(wx.getStorageSync('wxCode')){
      this.setData({
        truephone:wx.getStorageSync('truephone'),
        wxCode:wx.getStorageSync('wxCode')
      })
    }
  },
  getPhone:function(){
    var data = {
      'uid':wx.getStorageSync('uid')
    }
    $api.post('member/phone',data).then(res =>{
      if(res.data.code == 1){
        wx.switchTab({
          url: '../index/index'
        })
      }
    })
  },
  redOpen:function(){
    var money = Math.floor(Math.random()*2);
    if(money == 0){
      money = 1;
    }
    this.setData({
      dis:false,
      redtext:money+'.00',
    })
    this.addMoney(money);
  },
  addMoney:function(money){
    var that = this;
    var data = {
      'uid':wx.getStorageSync('uid'),
      'money':money}
    $api.post('money/add',data).then(res => {
      if(res.data.code == 1){
        wx.showToast({
          title: '领取红包成功可在个人中心,我的红包查看!!',
          duration:3000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading" 
       })
       that.setData({
        nexttrue:true
       })
      }else{
        wx.showToast({
          title: res.data.msg,
          duration:3000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading" 
        })
      }
    }).catch(err => {
        //请求失败
        //console.log('error','请求失败');
    })
  },
  toLogin:function(){
    this.setData({
      redShow:false
    })
    wx.navigateTo({
      url:'/pages/login/index'
    })
  },
  WritePhone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  bindKeyInput:function(e){
    this.setData({
      phoneCode:e.detail.value
    })
  },
  sendCode:function(){
    this.data.phone = this.data.phone.replace(/\s+/g, '');
    if(this.data.phone.length != 11){
      wx.showToast({
        title:'请输入正确手机号码',
        duration:3000,//显示时长
        mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
        icon:'none', //图标，支持"success"、"loading" 
      })
      return;
    }
    this.setData({
      sendShow:false
    })
    this.sendMessage();
  },
  sendMessage:function(){
    var that = this
    var data = {phone:this.data.phone}
    $api.post('messageSend',data).then(res =>{
      wx.showToast({
        title:res.data.msg,
        duration:3000,
        mask:true, 
        icon:'none',
      })
      if(res.data.code == 1){
        that.setData({
          wxCode:res.data.data,
          truephone:that.data.phone
        })
        wx.setStorageSync('truephone',that.data.phone)
        wx.setStorageSync('wxCode',res.data.data)
        that.timeclear();
      }
    }).catch(err => {
        //请求失败
        //console.log('error','请求失败');

    })
  },
  timeclear:function(){
    var that = this
    this.setData({
      //sendShow:false,
      time:60,
      sendTxt:'60s'
    })
    this.data.setInter = setInterval(function(){
      if(that.data.time == 0){
          that.setData({
              sendShow:true
          })
          clearInterval(that.data.setInter)
      }else{
          var numVal = that.data.time - 1;
          that.setData({
              sendTxt: numVal+'s',
              time:numVal
          });
      }
    }, 1000);
  },
  //更新手机号码
  phonesave:function(){
    var that = this
    if(!this.data.phoneCode){
      wx.showToast({
        title:'请输入验证码',
        duration:3000,
        mask:true, 
        icon:'none',
      })
      return;
    }
    if(this.data.phoneCode != this.data.wxCode){
      wx.showToast({
        title:'验证码错误!!',
        duration:3000,
        mask:true, 
        icon:'none',
      })
      return;
    }
    this.setData({
      phoneShow:false
    })
    var data ={
      'phone':this.data.truephone,
      'uid':wx.getStorageSync('uid')
    }
    $api.put('member/save',data).then(res => {
      if(res.data.code == 1){
        wx.removeStorageSync('truephone')
        wx.removeStorageSync('wxCode')
        wx.showToast({
          title:'验证成功!!!',
          duration:3000,
          mask:true, 
          icon:'none',
        })
        that.setData({
          phoneShow:false
          //redShow:true
        })
        wx.switchTab({
          url: '../index/index'
        })
      }
    })
  },
  back:function(){
    wx.switchTab({
      url: '../index/index'
    })
  }

})
